package com.hhit.bigscreenbackmanage.service.impl.jsgl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.PageUtil;
import com.hhit.bigscreenbackmanage.dao.jsgl.TJsglTysbyyMapper;
import com.hhit.bigscreenbackmanage.entity.jsgl.TJsglStrwjd;
import com.hhit.bigscreenbackmanage.entity.jsgl.TJsglSzjj;
import com.hhit.bigscreenbackmanage.entity.jsgl.TJsglTysbyy;
import com.hhit.bigscreenbackmanage.service.jsgl.TJsglTysbyyService;
import com.hhit.bigscreenbackmanage.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
public class TJsglTysbyyServiceImpl implements TJsglTysbyyService {

    private Logger log = LoggerFactory.getLogger(TJsglTysbyyServiceImpl.class);

    @Autowired
    TJsglTysbyyMapper tJsglTysbyyMapper;

    @Override
    public Map<String, Object> insertTJsglTysbyy(MultipartFile[] file, String dataTime, String propName) throws Exception {
        //删除同一时间数据
        JSONObject json = new JSONObject();
        json.put("dataTime",dataTime);
        tJsglTysbyyMapper.deleteTysbyyRecord(json);

        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TJsglTysbyy> list = (List<TJsglTysbyy>) Utils.mapToEntiry(result, TJsglTysbyy.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TJsglTysbyy tJsglTysbyy : list) {
                        tJsglTysbyy.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tJsglTysbyy.setDataTime(dataTime);
                    }
                    tJsglTysbyyMapper.insertTJsglTysbyy(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> queryTysbyyList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglTysbyyMapper.queryTysbyyNum(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());

        List<TJsglTysbyy> list = new ArrayList<>();
        try{
            list = tJsglTysbyyMapper.queryTysbyyList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }

        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public void deleteTysbyyRecord(JSONObject jsonObject) throws Exception {

        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }

        try {
            tJsglTysbyyMapper.deleteTysbyyRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }
}
